﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.HumanResources
{
    public class AddOnboardingReviewServices : ApplicationService, IAddOnboardingReviewServices
    {
        private readonly IRepository<TalentRegistration, int> _registrations;

        public AddOnboardingReviewServices(IRepository<TalentRegistration, int> registrations)
        {
            _registrations = registrations;
        }
        /// <summary>
        /// 添加人才登记
        /// </summary>
        /// <param name="humanResourcesDTO">人才登记</param>
        /// <returns></returns>
        public async Task<int> AddHuman(TalentRegistrationDTO humanResourcesDTO)
        {
            var entity = ObjectMapper.Map<TalentRegistrationDTO, TalentRegistration>(humanResourcesDTO);
            var result = await _registrations.InsertAsync(entity,true);
            return result == null ? 1 : 0; 
        }

        public async Task<int> DeleteHuman(int humanId)
        {
            var result = _registrations.DeleteAsync(m => m.Id == humanId);
            return result == null ? 1 : 0;
        }

        public async Task<TalentRegistrationDTO> GetHumanResources(int humanId)
        {
            var human = await _registrations.FindAsync(m => m.Id == humanId);
            var result = ObjectMapper.Map<TalentRegistration, TalentRegistrationDTO>(human);
            return result;
        }

        public async Task<List<TalentRegistrationDTO>> ShowHumanResources(ShowHumanResourcesDTO showHumanResourcesDTO)
        {
            var humanList = await _registrations.GetListAsync();
            humanList = humanList.WhereIf(!string.IsNullOrEmpty(showHumanResourcesDTO.EmployeeName), m => m.EmployeeName.Contains(showHumanResourcesDTO.EmployeeName)).ToList();
            humanList = humanList.WhereIf(!string.IsNullOrEmpty(showHumanResourcesDTO.EmployeeCode), m => m.EmployeeCode.Contains(showHumanResourcesDTO.EmployeeCode)).ToList();
            humanList = humanList.WhereIf(showHumanResourcesDTO.EducationBackground != null, m => m.EducationBackground == showHumanResourcesDTO.EducationBackground).ToList();
            humanList = humanList.WhereIf(showHumanResourcesDTO.Status != null, m => m.Status == showHumanResourcesDTO.Status).ToList();
            var result = ObjectMapper.Map<List<TalentRegistration>, List<TalentRegistrationDTO>>(humanList);
            return result;

        }

        public async Task<int> UpdateHuman(TalentRegistrationDTO humanResourcesDTO)
        {
            var entity = ObjectMapper.Map<TalentRegistrationDTO, TalentRegistration>(humanResourcesDTO);
            var result = await _registrations.UpdateAsync(entity);
            return result == null ? 1 : 0;
        }

        public async Task DelMany(List<int> humanIds)
        {
            if (humanIds == null || !humanIds.Any())
                return;
            await _registrations.DeleteDirectAsync(m => humanIds.Contains(m.Id));
        }
    }
}
